﻿using WMSSyncData.Utils;

namespace SyncHoldData
{
    class MyBll
    {
        public int GetSHHoldBoxes()
        {
            SqlDBHelper.Conn = "Data Source=192.168.9.75;Initial Catalog=WMS;User ID=WMS;Password=Password01!;";

            string sqlCmd = "TRUNCATE TABLE TABLE_HOLD;";
            sqlCmd += "INSERT INTO TABLE_HOLD SELECT * FROM SHMES_LINK_SN..SAA_MES.VIEW_HOLD;";
            return SqlDBHelper.ExcuteSQL(sqlCmd.ToString());
        }

        public int GetHYHoldBoxes()
        {
            SqlDBHelper.Conn = "Data Source=192.168.9.75;Initial Catalog=WMS_105;User ID=WMS;Password=Password01!;";
            string sqlCmd = "delete from TABLE_HOLD;";
            SqlDBHelper.ExcuteSQL(sqlCmd);
            //sqlCmd += "INSERT INTO TABLE_HOLD SELECT * FROM HYMES_LINK_SN..SAA_MES.VIEW_HOLD;";
            string holdCmd = "SELECT* FROM HYMES_LINK_SN..SAA_MES.VIEW_HOLD";
            var dt = SqlDBHelper.GetTable(holdCmd, 300);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string insertCmd = "";
                var CARTONNO = dt.Rows[i]["CARTONNO"].ToString();
                var STOCK = dt.Rows[i]["STOCK"].ToString();
                var MOCODE = dt.Rows[i]["MOCODE"].ToString();
                var ITEMCODE = dt.Rows[i]["ITEMCODE"].ToString();
                var VERSION = dt.Rows[i]["VERSION"].ToString();
                var DATECODE = dt.Rows[i]["DATECODE"].ToString();
                var QTY = (decimal)dt.Rows[i]["QTY"];

                insertCmd= "INSERT INTO TABLE_HOLD(CARTONNO,STOCK,MOCODE,ITEMCODE,VERSION,DATECODE,QTY) VALUES" +
                    "('{0}','{1}','{2}','{3}','{4}','{5}',{6});";
                insertCmd = string.Format(insertCmd, CARTONNO, STOCK, MOCODE, ITEMCODE, VERSION, DATECODE, QTY);
                SqlDBHelper.ExcuteSQL(insertCmd);
            }
            return dt.Rows.Count;
        }
    }
}
